package uiLayer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextPane;

import controllerLayer.CustomerCtr;
import controllerLayer.EmployeeCtr;
import controllerLayer.LoanCtr;
import controllerLayer.ProductCtr;

import modelLayer.Customer;
import modelLayer.Employee;
import modelLayer.ProductForLease;
import modelLayer.SaleLinesItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class DIYG extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JButton btnEnter;
	private JLabel lblEmployeesCpr;
	private JRadioButton rdbtnLoan;
	private JTextField textField_1;
	private JLabel lblId;
	private JLabel lblQuantity;
	private JTextField textField_2;
	private JButton btnEnter_1;
	private JTextPane textPane;
	private JButton btnEnter_2;
	private JLabel lblDiy;
	private JTextField textField_3;
	private JLabel lblCustomersCpr;
	private JButton btnEnter_3;
	private Employee employee;
	private EmployeeCtr ectr;
	private Customer customer;
	private CustomerCtr cctr;
	private static InputParser inp;
	private ProductCtr pctr;
	private ArrayList<SaleLinesItem> shoppingCart;
	private LoanCtr lctr;
	private ProductForLease pfl;
	private String borrowDate;
	private int period;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DIYG frame = new DIYG();
					inp.load();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DIYG() {
		pctr = new ProductCtr();
		cctr = new CustomerCtr();
		ectr = new EmployeeCtr();
		lctr = new LoanCtr();
		inp = InputParser.getInstance();
		shoppingCart = new ArrayList<SaleLinesItem>();
		pfl = null;
		setTitle("DIY");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 680, 520);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		btnEnter = new JButton("Enter");
		btnEnter.addActionListener(new ActionListener() {
			

			public void actionPerformed(ActionEvent e) {
				String empId = textField.getText();
				try{
					employee = ectr.getEmployee(empId);
					textPane.setText(employee.toString());
					textField.setEnabled(false);
				}
				catch (NullPointerException npe){
					textPane.setText("Employee doesn't exist. Try again.");
				}
			}
		});
		
		lblEmployeesCpr = new JLabel("Employee's CPR:");
		
		rdbtnLoan = new JRadioButton("Loan");
		rdbtnLoan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnLoan.isSelected()){
					btnEnter_2.setText("Loan");
					lblQuantity.setText("Period:");
				}
				else {
					btnEnter_2.setText("Sell");
					lblQuantity.setText("Quantity:");
				}
			}
		});
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		lblId = new JLabel("ID:");
		
		lblQuantity = new JLabel("Quantity:");
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		btnEnter_1 = new JButton("Enter");
		btnEnter_1.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				if(rdbtnLoan.isSelected()){
					DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
					Date date = new Date();
					int Id = InputParser.getInstance().parseInt(textField_1.getText());
					int period = InputParser.getInstance().parseInt(textField_2.getText());
					String borrowDate = dateFormat.format(date);
					date.setDate(date.getDate() + period);
					String returnDate = dateFormat.format(date);
					try{
						pfl = lctr.getProduct(Id);
						textPane.setText(pfl.toString() + "\n" + borrowDate+ " - " + returnDate);
					}
					catch(NullPointerException npe){
						
							textPane.setText("Product doesn't exist!");
						}
				}
				else{
				String ID = textField_1.getText();
				double quantity = inp.parseDouble(textField_2.getText());												
				try{
				textPane.setText(pctr.getSaleLinesItem(ID).toString()+ "................" + quantity + pctr.getSaleLinesItem(ID).getMeasure());
				pctr.countQuantity(ID, quantity);
				shoppingCart.add(pctr.getSaleLinesItem(ID));
				}
				
				catch(NullPointerException npe ){
					textPane.setText("Item doesn't exist");
				}
				
					catch (IllegalArgumentException iae){
						textPane.setText(iae.getMessage());
					}
			}
			}});
		
		textPane = new JTextPane();
		
		btnEnter_2 = new JButton("Sell");
		btnEnter_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnLoan.isSelected()){
					int re = JOptionPane.showConfirmDialog(btnEnter_2,"             Are you sure?","Loan Product",0,2);
					if(re==0){
					lctr.loanProduct(customer.getId(), pfl.getId(), employee.getId(), borrowDate, period);
					textPane.setText(pfl.getName()+" has been loaned");
					}
				}
				else{
					pctr.sellProduct(employee.getId(), customer.getId(), shoppingCart);
					textPane.setText("");
					double total = 0;
					int re = JOptionPane.showConfirmDialog(btnEnter_2,"             Are you sure?","Sell Product",0,2);
					if(re==0){
					for(SaleLinesItem sli: shoppingCart){
						textPane.setText(textPane.getText()+"\n"+sli.getName()+ " " + sli.getBuyQuantity() + " price" + sli.getProduct().getPrice()*sli.getBuyQuantity());
						total = total + sli.getProduct().getPrice() * sli.getBuyQuantity();
					}
					textPane.setText(textPane.getText()+"\ntotal"+ total);
					}
				}
			}
		});
		
		lblDiy = new JLabel("DIY");
		lblDiy.setFont(new Font("Georgia", Font.PLAIN, 28));
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		
		lblCustomersCpr = new JLabel("Customer's CPR:");
		
		btnEnter_3 = new JButton("Enter");
		btnEnter_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String custId = textField_3.getText();
				try{
					customer = cctr.getCustomer(custId);
					textPane.setText(customer.toString());
					textField_3.setEnabled(false); 
					}
					catch (NullPointerException npe){
						textPane.setText("Customer doesn't exist. Try again.");
					}
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(29)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(textPane, GroupLayout.PREFERRED_SIZE, 547, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnEnter_2))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblDiy, GroupLayout.PREFERRED_SIZE, 246, GroupLayout.PREFERRED_SIZE)
							.addGap(122)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblEmployeesCpr)
								.addComponent(lblCustomersCpr))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(textField_3, 0, 0, Short.MAX_VALUE)
								.addComponent(textField, GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(btnEnter)
								.addComponent(btnEnter_3)))
						.addComponent(rdbtnLoan)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblId)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 266, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblQuantity)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(6)
							.addComponent(btnEnter_1)))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblEmployeesCpr)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnEnter))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblCustomersCpr)
								.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnEnter_3)))
						.addComponent(lblDiy, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(rdbtnLoan)
							.addGap(4)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(4)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblId)
										.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(1)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblQuantity)
										.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnEnter_1))))
							.addGap(6)
							.addComponent(textPane, GroupLayout.PREFERRED_SIZE, 315, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnEnter_2))
					.addGap(20))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
